const { throttle } = require('../../modules/function_throttle');

let timer = null;
Component({
    properties: {
        index: {
            type: Number,
            value: 0
        },
        question: {
            type: Object,
            value() {
                return {};
            }
        },
        themeColor: {
            type: String,
            value: getApp().globalData.themeColor
        }
    },
    data: {
        date: null
    },
    ready() {
        let now = new Date();
        const end = now.format('yyyy-MM-dd');

        now.setFullYear(now.getFullYear() - 100);
        now.setMonth(0);
        now.setDate(1);
        const begin = '1920-01-01';
        this.setData({
            date_begin: begin,
            date_end: end
        });
    },
    methods: {
        onPickerChange(evt) {
            console.log(evt);
            this.setData({
                date: evt.detail.value
            });
        },
        onConfirm: throttle(function () {
            let value = this.data.date.split('-');
            value = `${value[0]}${value[1]}${value[2]}`;
            // console.log(timer);
            this.triggerEvent('change', {
                index: this.data.index,
                value: value
            });
            // if (timer !== null) {
            //     return;
            // } else {
            //     timer = setTimeout(() => {
            //         clearTimeout(timer);
            //         timer = null;
            //         this.triggerEvent('change', {
            //             index: this.data.index,
            //             value: value
            //         });
            //     }, 1000);
            // }
        }, 1000)
    }
});
